<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Jwt\JwtController;
use App\Http\Server\last;
use App\Models\Bargain;
use App\Models\Bargain_Help;
use App\Models\Bargain_User;
use App\Service\Alg;
use App\Service\CommonService;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class HaggleController extends Controller
{
    /**s
     * @return array
     * User : 邢洋洋
     * Data : 2022/11/14
     * Time : 20:07
     * 砍价商品
     */
    public function bargain(){
        try {
            //模型获取数据
            $bargainData = Bargain::get();
            //返回信息
            return CommonService::success(200,'砍价商品查询成功',$bargainData);
        }catch (\Exception $exception){
            return CommonService::fail(9999,$exception->getMessage(),'');
        }

    }

    /**
     * @param Request $request
     * @return array
     * User : 邢洋洋
     * Data : 2022/11/14
     * Time : 20:07
     * 倒计时
     */
    public function getBargainTime(Request $request){
        try {
            $id = intval($request->input('product_id'));
            $bargainData = Bargain::where(['product_id'=>$id])->first();
            $stopTime = $bargainData['stop_time'];
            $nowTime = time();
            $time = $stopTime-$nowTime;
            return CommonService::success(200,'剩余时间(秒)',$time);
        }catch (\Exception $exception){
            return CommonService::fail(9999,$exception->getMessage(),'');
        }
    }

    /**
     * @param Request $request
     * @return array
     * User : 邢洋洋
     * Data : 2022/11/14
     * Time : 20:07
     * 砍价详情
     */
    public function bargainDetail(Request $request){
        try {
            $id = intval($request->input('product_id'));
            $uid = intval($request->header('uid'));
            $bargainDetail = Bargain::where(['product_id'=>$id,'uid'=>$uid])->with(['bargain_user'])->first();
            return CommonService::success(200,'砍价详情查询成功',$bargainDetail);
        }catch (\Exception $exception){
            return CommonService::fail(9999,$exception->getMessage(),'');
        }
    }

    /**
     * @param Request $request
     * @return array
     * User : 邢洋洋
     * Data : 2022/11/14
     * Time : 20:07
     * 自己砍第一刀
     */
    public function startBargian(Request $request){
        $id = intval($request->input('product_id'));
        $uid = intval($request->header('uid'));
        $bargainData = Bargain::where(['product_id'=>$id,'uid'=>$uid])->first();
        //判断时间是否结束
        //if($bargainData['stop_time']<=time()) return CommonService::fail(9999,'活动已结束','');
        //判断用户是否砍过这个商品
        $bargainUser = Bargain_User::where(['uid'=>$uid,'bargain_id'=>$id])->first();
        if($bargainUser) return CommonService::success(200,'您已参与这个商品的砍价',$bargainUser['id']);
        //没有砍过添加砍价记录
        $info =Bargain_User::create([
            'uid'=>$uid,
            'bargain_id'=>$id,
            'bargain_price_min'=>$bargainData['min_price'],
            'bargain_price'=>$bargainData['price']-$bargainData['min_price'],
            'add_time'=>time(),
            'status'=>1,
            'command'=>Alg::Word()
        ]);
        //自己砍第一刀
        $bargainHelp = Bargain_Help::create([
            'uid'=>$uid,
            'bargain_id'=>$id,
            'bargain_user_id'=>$info['id'],
            'price'=>$info['bargain_price'] * 0.7,
            'add_time'=>time(),
        ]);
        Bargain_User::where(['uid'=>$uid,'bargain_id'=>$id])->update([
            'price'=>$bargainHelp['price'],
        ]);
        return CommonService::success(200,'参与成功',$info['status']);
    }

    /**
     * @param Request $request
     * @return array
     * User : 邢洋洋
     * Data : 2022/11/14
     * Time : 19:54
     * 邀请
     */
    public function bargainInvitation(Request $request){
        $id = intval($request->input('bargain_id'));
        $uid = intval($request->header('uid'));
        $data = Bargain_User::where(['bargain_id'=>$id,'uid'=>$uid])->first();
        return CommonService::success(200,'邀请成功',$data['command']);
    }

    /**
     * @param Request $request
     * @return array
     * User : 邢洋洋
     * Data : 2022/11/14
     * Time : 20:07
     * 砍价记录
     */
    public function bargainRecord(Request $request){
        $id = intval($request->input('product_id'));
        //高并发处理 取出下标0-10的数据
        $bargainData = Redis::lrange('help',0,10);
        $bargainList = Bargain_Help::where('bargain_id',$id)->orderBy('price','desc')->get();
        return CommonService::success(200,'砍价记录',$bargainList);
    }

    /**
     * @param Request $request
     * @return array
     * User : 邢洋洋
     * Data : 2022/11/14
     * Time : 20:07
     * 帮砍
     */
    public function helpCut(Request $request){
        try {
            $command = strval($request->input('command'));
            $uid = intval($request->input('uid'));
            $bargainUser = Bargain_User::where('command',$command)->first();
            if(!$bargainUser) return CommonService::fail(5001,'口令不存在','');
            $bargainHelp = Bargain_Help::where(['bargain_user_id'=>$bargainUser['id'],'uid'=>$uid])->first();
            if($bargainHelp) return CommonService::fail(5001,'您已帮助此用户砍过这个商品','');
            $bargainData = Bargain::where('product_id',$bargainUser['bargain_id'])->first();
            //帮砍成功存入redis 存的是帮砍的用户id
            Redis::rpush('help',$uid);
            //调用
            $info = \App\Service\last::lastDao($bargainData,$bargainUser,$command,$uid);
            return CommonService::success(200,'帮砍成功',$info);
        }catch (\Exception $exception){
            return CommonService::fail(9999,$exception->getMessage(),'');
        }

    }
}
